// Fill out your copyright notice in the Description page of Project Settings.


#include "BaseCharacter.h"
#include "GameFramework\SpringArmComponent.h"
#include "Camera\CameraComponent.h"
#include "GameFramework\CharacterMovementComponent.h"

// Sets default values
ABaseCharacter::ABaseCharacter()
{
 	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	SpringArmComb = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComb"));
	SpringArmComb->TargetArmLength = 400;
	SpringArmComb->bUsePawnControlRotation = true;
	SpringArmComb->SetupAttachment(RootComponent);

	CameraComb = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComb"));
	CameraComb->bUsePawnControlRotation = false;
	CameraComb->SetupAttachment(SpringArmComb,USpringArmComponent::SocketName);

	bUseControllerRotationPitch = false;
	bUseControllerRotationYaw = false;
	bUseControllerRotationRoll = false;

	BaseTurnRate = 65;
	BaseLookupRate = 65;
	WalkSpeed = 300;
	RunSpeed = 600;

	GetCharacterMovement()->bOrientRotationToMovement = true;
	GetCharacterMovement()->RotationRate = FRotator(0.f,540,0);
	GetCharacterMovement()->JumpZVelocity = 650;
	GetCharacterMovement()->AirControl = 0.2f;
	GetCharacterMovement()->MaxWalkSpeed = 300;

}

// Called when the game starts or when spawned
void ABaseCharacter::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void ABaseCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

// Called to bind functionality to input
void ABaseCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	PlayerInputComponent->BindAxis("MoveForward",this,&ABaseCharacter::MoveForward);
	PlayerInputComponent->BindAxis("MoveRight",this,&ABaseCharacter::MoveRight);
	PlayerInputComponent->BindAxis("TurnAtRate",this,&ABaseCharacter::TurnAtRate);
	PlayerInputComponent->BindAxis("LookAtRate",this,&ABaseCharacter::LookAtRate);

	PlayerInputComponent->BindAction("Jump",IE_Pressed,this,&ABaseCharacter::Jump);
	PlayerInputComponent->BindAction("Jump",IE_Released,this,&ABaseCharacter::Jump);
}

void ABaseCharacter::MoveForward(float value) 
{
	const FRotator Rotation = Controller->GetControlRotation();
	const FRotator YawRotation(0.f,Rotation.Yaw,0.f);
	const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
	AddMovementInput(Direction,value);
}

void ABaseCharacter::MoveRight(float value) 
{
	if (Controller &&  value != 0)
	{
		const FRotator Rotation = Controller->GetControlRotation();
		const FRotator YawRotation(0.f,Rotation.Yaw,0.f);
		const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
		AddMovementInput(Direction,value);
	}
}

void ABaseCharacter::TurnAtRate(float value) 
{
	AddControllerYawInput(value * BaseTurnRate * GetWorld()->DeltaTimeSeconds);
}

void ABaseCharacter::LookAtRate(float value) 
{
	AddControllerPitchInput(value * BaseLookupRate * GetWorld()->DeltaTimeSeconds);
}

